package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Rider;
import com.draven.mall.service.RiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 骑手用户控制器
 */
@RestController
@RequestMapping("/api/rider")
public class RiderUserController {

    private static final Logger log = LoggerFactory.getLogger(RiderUserController.class);

    @Autowired
    private RiderService riderService;

    /**
     * 获取骑手个人信息
     */
    @GetMapping("/info")
    public ApiResponse getRiderInfo(@RequestParam Long riderId) {
        if (riderId == null) {
            return ApiResponse.error("骑手ID不能为空");
        }
        
        try {
            Rider rider = riderService.getById(riderId);
            if (rider == null) {
                return ApiResponse.error("骑手不存在");
            }
            
            // 构建返回数据，过滤敏感字段
            Map<String, Object> riderInfo = new HashMap<>();
            riderInfo.put("id", rider.getId());
            riderInfo.put("username", rider.getUsername());
            riderInfo.put("realName", rider.getRealName());
            riderInfo.put("phone", rider.getPhone());
            riderInfo.put("avatar", rider.getAvatar());
            riderInfo.put("status", rider.getStatus());
            riderInfo.put("workingStatus", rider.getWorkingStatus());
            riderInfo.put("grabOrderStatus", rider.getGrabOrderStatus());
            riderInfo.put("createdTime", rider.getCreatedTime());
            
            return ApiResponse.success(riderInfo);
        } catch (Exception e) {
            log.error("获取骑手信息失败", e);
            return ApiResponse.error("获取骑手信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新骑手工作状态
     */
    @PostMapping("/status/update")
    public ApiResponse updateWorkStatus(
            @RequestParam Long riderId,
            @RequestParam Integer status) {
        
        log.info("更新骑手工作状态: riderId={}, status={}", riderId, status);
        
        if (riderId == null) {
            return ApiResponse.error("骑手ID不能为空");
        }
        
        if (status == null || (status != 0 && status != 1)) {
            return ApiResponse.error("状态值无效，必须是0或1");
        }
        
        try {
            Rider rider = riderService.getById(riderId);
            if (rider == null) {
                return ApiResponse.error("骑手不存在");
            }
            
            // 更新骑手工作状态
            rider.setWorkingStatus(status);
            rider.setUpdatedTime(new Date());
            boolean updated = riderService.updateById(rider);
            
            if (updated) {
                String statusText = status == 1 ? "工作中" : "休息中";
                log.info("骑手{}(ID:{})状态已更新为{}", rider.getUsername(), riderId, statusText);
                return ApiResponse.success("状态已更新为" + statusText);
            } else {
                log.warn("更新骑手状态失败: riderId={}", riderId);
                return ApiResponse.error("更新状态失败");
            }
        } catch (Exception e) {
            log.error("更新骑手状态异常", e);
            return ApiResponse.error("系统异常: " + e.getMessage());
        }
    }
    
    /**
     * 更新骑手个人信息
     */
    @PostMapping("/profile/update")
    public ApiResponse updateProfile(
            @RequestParam Long riderId,
            @RequestParam(required = false) String avatar,
            @RequestParam(required = false) String phone) {
        
        if (riderId == null) {
            return ApiResponse.error("骑手ID不能为空");
        }
        
        try {
            Rider rider = riderService.getById(riderId);
            if (rider == null) {
                return ApiResponse.error("骑手不存在");
            }
            
            boolean needUpdate = false;
            
            // 更新头像
            if (avatar != null && !avatar.isEmpty()) {
                rider.setAvatar(avatar);
                needUpdate = true;
            }
            
            // 更新电话
            if (phone != null && !phone.isEmpty()) {
                rider.setPhone(phone);
                needUpdate = true;
            }
            
            if (needUpdate) {
                rider.setUpdatedTime(new Date());
                boolean updated = riderService.updateById(rider);
                
                if (updated) {
                    return ApiResponse.success("个人信息更新成功");
                } else {
                    return ApiResponse.error("更新失败");
                }
            } else {
                return ApiResponse.success("没有信息需要更新");
            }
        } catch (Exception e) {
            log.error("更新骑手信息异常", e);
            return ApiResponse.error("系统异常: " + e.getMessage());
        }
    }
} 